package com.quanyan.financialSystem.service.place.impl;

import com.lefu.commons.utils.Page;
import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.financialSystem.entity.db.TbBizPlaceBuyContractDetail;
import com.quanyan.financialSystem.entity.db.TbBizPlaceBuyContractDetailExample;
import com.quanyan.financialSystem.entity.vo.req.ReqPlaceRentCost;
import com.quanyan.financialSystem.entity.vo.resp.BuyContractDetail;
import com.quanyan.financialSystem.entity.vo.resp.RespPlaceInComeStream;
import com.quanyan.financialSystem.entity.vo.resp.RespPlaceRentCost;
import com.quanyan.financialSystem.mapper.PlaceRentCostMapper;
import com.quanyan.financialSystem.mapper.TbBizPlaceBuyContractDetailMapper;
import com.quanyan.financialSystem.service.place.PlaceRentCostService;
import com.quanyan.place.entity.TbBizPlaceBuyContract;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/19.
 */
@Service
public class PlaceRentCostServiceImpl implements PlaceRentCostService {

    private static final Logger logger = LoggerFactory.getLogger(PlaceRentCostServiceImpl.class);

    @Autowired
    private PlaceRentCostMapper placeRentCostMapper;



    @Override
    public APIResponse<PageObj<List<RespPlaceRentCost>>> findPlaceRentCost(ReqPlaceRentCost reqPlaceRentCost) {
        logger.info("开吃查询场地收入流水----》{}",reqPlaceRentCost.toString());
        Page<RespPlaceRentCost> page = new Page<RespPlaceRentCost>();
        int pageIndex = reqPlaceRentCost.getPageNo();
        page.setCurrentPage(pageIndex);
        page.setShowCount(reqPlaceRentCost.getPageSize());
        List<RespPlaceRentCost> lstResult = placeRentCostMapper.findAllPlaceRentCost(page, reqPlaceRentCost);
        if(pageIndex > page.getTotalPage()){
            lstResult = new ArrayList<>();
            page.setTotalPage(0);
            page.setTotalResult(0);
        }

        if(lstResult !=null && lstResult.size() > 0){
            List<BuyContractDetail> lstContractDetail = placeRentCostMapper.findContractDetail(lstResult);
                for (BuyContractDetail d : lstContractDetail){
                    for (RespPlaceRentCost respPlaceRentCost : lstResult){
                        if(respPlaceRentCost.getContractId() != null){
                            if(d.getAttractId().equals(respPlaceRentCost.getContractId().toString())){
                                respPlaceRentCost.getLstDetail().add(d.toString());
                            }
                        }

                    }
                }
        }
        logger.info("结束查询场地收入流水---》{}",lstResult.toString());
        PageObj<List<RespPlaceRentCost>> result = PageObj.create(page.getTotalResult(), page.getCurrentPage(), page.getShowCount(), lstResult);
        return APIResponse.returnSuccess(result);
    }
}
